package com.yfp.operation.controller;


import com.yfp.common.core.domain.R;
import com.yfp.operation.domain.OrderDetailDO;
import com.yfp.operation.domain.RedisOrderDetail;
import com.yfp.operation.service.*;
import com.yfp.operation.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/job")
@Tag(name = "定时任务接口")
@Slf4j
public class JobController {

    @Autowired
    private MachineService machineService;

    @Resource
    private UserBillService userBillService;

    @Resource
    private DeviceService deviceService;

    @Resource
    private UserWithdrawService userWithdrawService;

    @Autowired
    private MarAssociationService marAssociationService;
    @Resource
    private ClientCouponService clientCouponService;
    @Resource
    private MarCouponService marCouponService;
    @Resource
    private MarActivityService marActivityService;
    @Resource
    private MarCouponPackService marCouponPackService;
    @Resource
    private ClientService clientService;
    @Resource
    private ActivitySignInsService activitySignInsService;
    @Resource
    private LotteryRecordsService lotteryRecordsService;

    @Operation(summary = "检测设备健康状况")
    @GetMapping("/check/meal/anomaly")
    public R<Void> checkMachineMealAnomaly() {
        machineService.checkMachineMealAnomaly();
        return R.ok();
    }

    @Operation(summary = "添加缓存生产订单-小程序")
    @PostMapping("/check/meal/min/anomaly")
    public R<Void> checkMachineMealMinAnomaly(@RequestBody List<RedisOrderDetail> redisOrderDetails) {
        log.info("/check/meal/min/anomaly 添加生产订单：{}", redisOrderDetails);
        List<OrderDetailDO> orderDetailDOs = new ArrayList<>();
        OrderDetailDO orderDetailDO = null;
        for (RedisOrderDetail redisOrderDetail : redisOrderDetails) {
            orderDetailDO = new OrderDetailDO();
            orderDetailDO.setOrderDetailId(redisOrderDetail.getDetailId());
            orderDetailDO.setQueueTime(redisOrderDetail.getQueueTime());
            orderDetailDOs.add(orderDetailDO);
        }
        deviceService.setMachineOrderDetail(orderDetailDOs);
        return R.ok();
    }

    @Operation(summary = "订单入账", description = "定时任务每天凌晨2点执行")
    @GetMapping("/orderBill/carryto")
    public R<Void> orderBillCarryto() {
        userBillService.orderBillCarryto();
        return R.ok();
    }

    @Operation(summary = "服务商提现", description = "定时任务每天凌晨3点执行")
    @GetMapping("/provider/withdraw")
    public R<Void> providerWithdraw() {
        userWithdrawService.providerWithdraw();
        return R.ok();
    }

    @Operation(summary = "自动打款分账", description = "定时任务每天22点执行")
    @GetMapping("/orderBill/autoMakePayment")
    public R<Void> autoMakePayment() {
        userBillService.autoMakePayment();
        return R.ok();
    }

    @Operation(summary = "同步微信群列表")
    @GetMapping("/marAssociation/syncGroupChat")
    public R<Void> syncGroupChat() throws WxErrorException {
        marAssociationService.syncGroupChat();
        return R.ok();
    }

    @Operation(summary = "调用pg存储过程，更新表状态")
    @GetMapping("/mar/status")
    public R<Void> marCall() {
//        log.info("marCall:调用pg存储过程，更新表状态");
        clientCouponService.callUpdateClientCouponStatus();
        marCouponService.callUpdateMarCouponStatus();
        marActivityService.callUpdateMarActivityStatus();
        return R.ok();
    }

    @Operation(summary = "检测并更新所有优惠券礼包状态")
    @GetMapping("/check/couponPack/status")
    public R<Void> checkCouponPackAndUpdateAllStatus() {
        // 检测并更新所有优惠券礼包状态
        marCouponPackService.checkAndUpdateAllStatus();
        return R.ok();
    }

    @Operation(summary = "用户打标签")
    @GetMapping("/client/label")
    public R<Void> clientLabel() {
        // 用户打标签
        clientService.clientLabel();
        return R.ok();
    }

    @Operation(summary = "检测并更新所有打卡活动状态")
    @GetMapping("/check/activitySignIns/status")
    public R<Void> checkActivitySignInsAndUpdateAllStatus() {
        // 检测并更新所有打卡活动状态
        activitySignInsService.checkAndUpdateAllStatus();
        return R.ok();
    }


    @Operation(summary = "检测并更新所有抽奖活动状态")
    @GetMapping("/check/lottery/status")
    public R<Void> checkLotteryAndUpdateAllStatus() {
        // 检测并更新所有打卡活动状态
        lotteryRecordsService.checkLotteryAndUpdateAllStatus();
        return R.ok();
    }


    @PutMapping("/client/coupon/storedCoupon/expire")
    @Operation(summary = "给进社群的新客户打点位标签")
    R<Void> newClientPositionLabel(){
        clientService.newClientPositionLabel();
        return R.ok();
    }



}
